//
//  NewCollectionViewCell.m
//  A_HandHeldLOL
//
//  Created by Zhao YanBin on 16/1/19.
//  Copyright © 2016年 zhaoyanbin. All rights reserved.
//

#import "NewCollectionViewCell.h"
#import "AFNModel.h"
#import "NewViewController.h"
#import <UIImageView+WebCache.h>
#import "NewModel.h"
#import "NewTableViewCell.h"
#import "CarouselPicture.h"
#import <MJRefresh.h>
#import "NewDianjiViewController.h"
#import "LunbodianjiViewController.h"
@interface NewCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewAccessibilityDelegate,UIScrollViewDelegate>


@property(nonatomic, retain )NSMutableArray *dataArr;
@property(nonatomic, retain)UITableView *tableView;

//头部
@property(nonatomic, retain)UIView *header;

//轮播图
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, retain)NSMutableArray *lunboArr;

@property(nonatomic, retain)NSIndexPath *indexpath;

@property(nonatomic, assign)NSInteger number;

@end
@implementation NewCollectionViewCell
-(void)dealloc
{
    [self.tableView release];
    [self.dataArr release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createtableView];
    }
    return self;
}

-(void)setUrlStr:(NSString *)urlStr
{
    [_urlStr release];
    _urlStr = [urlStr copy];
    
    [AFNModel GETWithURL:urlStr completion:^(id result) {
        self.dataArr = result[@"list"];
        self.dataArr = [NewModel modelHanderWithArray:_dataArr];
        [self.tableView reloadData];
    }];
}

//-(UITableView *)setWithUrlStr:(NSString *)urlstr
//{
//    [AFNModel GETWithURL:urlstr completion:^(id result) {
//        self.dataArr = result[@"list"];
//        self.dataArr = [NewModel modelHanderWithArray:_dataArr];
//        [self.tableView reloadData];
//    }];
//    return _tableView;
//}
- (void)createtableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.contentView.frame.size.width, self.contentView.frame.size.height - 49 - 40) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_tableView];
    
    
    //注册重用池
    [self.tableView registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"cellthree"];
    self.header = [[UIView alloc] initWithFrame:CGRectMake(0, -44, 375, 200)];
    _tableView.tableHeaderView = _header;
    [_tableView release];
    
    //创建轮播图
    [self createscrollview];
    
    [self zuixinxiala];
}
- (void)createscrollview
{
    //轮播图
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, -44, self.contentView.frame.size.width, 200)];
    [_header addSubview:_scroll];
    //    _scroll.bounces = NO;
    //    _scroll.showsHorizontalScrollIndicator = NO;
        [self.scroll release];
    //横向滚动
    self.scroll.contentSize= CGSizeMake(self.contentView.frame.size.width * 5, 200);
    //整页翻动
    
    self.scroll.pagingEnabled = YES;
    
    //设置代理人
    self.scroll.delegate = self;
    self.scroll.tag = 2000;
    //开启交互
    _scroll.userInteractionEnabled = YES;
    
    //轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    tap.numberOfTouchesRequired = 1;
    tap.numberOfTapsRequired = 1;
    [_scroll addGestureRecognizer:tap];
    [tap release];

    //网络请求
    NSString *str = @"http://qt.qq.com/static/pages/news/phone/c13_list_1.shtml";
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"list"];
        self.lunboArr = [NewModel modelHanderWithArray:arr];
        [self.tableView reloadData];
        
        
        NSMutableArray *photoArr = [NSMutableArray array];
        
        for (NSDictionary *dic in arr) {
            [photoArr addObject:dic[@"image_url_big"]];
        }
        
        //调用封装
        CarouselPicture *lunBo = [[CarouselPicture alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 200) timeInterval:2 imageUrlArr:photoArr urlArr:nil];
        
        [_header addSubview:lunBo];
        [lunBo release];
        
    }];
    
}
//轻拍手势实现
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    
    
}

//上拉加载  下拉刷新
-(void)zuixinxiala
{
    
    self.number = 0;
    //上拉加载
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(zuixinheader)];
    //结束刷新
    [self.tableView.mj_header beginRefreshing];
    
    //下拉刷新
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(zuixinshangla)];
    
    if (_dataArr.count != 0) {
        //开始刷新
        [_tableView.mj_footer beginRefreshing];
    }
}

//上拉加载
//-(void)zuixinshangla
//{
////    [_tableView.mj_footer beginRefreshing];
//    
//    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://qt.qq.com/static/pages/news/phone/c13_list_%ld.shtml", self.number] completion:^(id result) {
//        NSArray *arrTemp = [NewModel modelHanderWithArray:result[@"list"]];
//        for (NSInteger i = 0; i < 20; i++) {
//            [self.dataArr addObject:arrTemp[i]];
//        }        [_tableView reloadData];
//    }];
//    [self.tableView.mj_footer endRefreshing];
//   self.number += 1;
//}


//下拉刷新
-(void)zuixinheader
{
    NSString *str = @"http://qt.qq.com/static/pages/news/phone/c13_list_1.shtml";
    [AFNModel GETWithURL:str completion:^(id result) {
        self.dataArr = [NSMutableArray array];
        NSArray *arr = result[@"list"];
        self.dataArr = [NewModel modelHanderWithArray:arr];
        
        //        刷新时间
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
            
            // 刷新collectionview
            [self.tableView reloadData];

            
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.tableView.mj_header endRefreshing];
//        });
    }];
}


-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellthree" forIndexPath:indexPath];
    
    NewModel *m = _dataArr[indexPath.row];
    cell.title.text = m.title;
    cell.subhead.text = m.summary;
    cell.time.text = m.publication_data;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.image_url_small]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *m = _dataArr[indexPath.row];
    
    [self.delegate passValue:m.article_url];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100;
}


@end
